package com.yylending.las.lending.quota.baseinfo;

import android.text.TextUtils;
import com.google.gson.Gson;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.secure.Cryptos;
import com.yylending.las.Global;
import com.yylending.las.common.AESEntity;
import com.yylending.las.Constants;
import com.yylending.las.lending.quota.entity.CustomerBean;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * P:获取基本信息.
 * Created by ying_he on 2017/6/28.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class CustomerInfoPresenter extends Presenter<CustomerInfoModel, CustomerInfoPresenter.View> {

	private final static String TAG = CustomerInfoPresenter.class.getSimpleName();
	private static final String MSG_REAL_NAME_FAILED = "获取信息失败";

	private View mView;
	private String mRequestPage;

	public static class Generator implements PresenterFactory<CustomerInfoPresenter> {
		@Override
		public CustomerInfoPresenter create() {
			return new CustomerInfoPresenter();
		}
	}

	public interface View extends IView {
		void customerInfoExist(String type, CustomerBean bean);

		void customerInfoNotExist(String type, String msg);
		/**
		 * 请求服务器返回异常
		 * @param type
		 * @param msg
		 */
		void getCustomerInfoError(String type, String msg);
	}

	@Override
	public void attach(CustomerInfoPresenter.View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new CustomerInfoModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	/**
	 * 获取身份证信息
	 */
	public void getCustomerInfo(String type) {
		CustomerInfoModel.CustomerInfoParams params = new CustomerInfoModel.CustomerInfoParams();
		params.type = type;
		execute(new AESEntity(params), CustomerInfoModel.METHOD_CUSTOMER_INFO, true, type, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getCustomerInfoResult(CustomerInfoModel.CustomerInfoResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			String type = (String) result.extraParam;
			if (BaseResultEntity.isResponseSuccess(result)) {
				if (result.getData() != null && !TextUtils.isEmpty(result.getData().json)) {
					CustomerInfoModel.AesData data = result.getData();
					CustomerBean customerBean = new Gson().fromJson(Cryptos.aesDecrypt(data.json, Global.KEY_AES), CustomerBean.class);
					CLog.i(TAG, "服务器基本信息="+Cryptos.aesDecrypt(data.json, Global.KEY_AES));
					switch (type) {
						case Constants.CUSTOMER_TYPE_IDENTITY:
							if (null == customerBean || null == customerBean.identity || TextUtils.isEmpty(customerBean.identity.inumber) || TextUtils.isEmpty(customerBean.identity.iname)) {
								Global.LOGIN_REAL_NAME = "";
								mView.customerInfoNotExist(type, "获取身份证信息失败");
							} else {
								CLog.i(TAG, "getBaseInfo customerBean.identity=" + customerBean.identity.toString());
								Global.LOGIN_REAL_NAME = customerBean.identity.iname;
								mView.customerInfoExist(type, customerBean);
							}
							break;
						case Constants.CUSTOMER_TYPE_BASEINFO:
							if (null == customerBean || null == customerBean.baseInfo || TextUtils.isEmpty(customerBean.baseInfo.addP) || TextUtils.isEmpty(customerBean.baseInfo.addC)) {
								mView.customerInfoNotExist(type, "基本信息不存在");
							} else {
								CLog.i(TAG, "getBaseInfo baseInfo=" + customerBean.baseInfo.toString());
								mView.customerInfoExist(type, customerBean);
							}
							break;
						case Constants.CUSTOMER_TYPE_SFACE:
							if (null != customerBean && null != customerBean.sface && Constants.FACE_VALID.equals(customerBean.sface.status)) {
								CLog.i(TAG, "服务器基本信息=刷脸有效");
								mView.customerInfoExist(type, customerBean);
							}else{
								mView.customerInfoNotExist(type, TextUtils.isEmpty(result.getRetMsg()) ? MSG_REAL_NAME_FAILED : result.getRetMsg());
							}
							break;
						case Constants.CUSTOMER_TYPE_ADDINFO:
							if (null != customerBean && null != customerBean.addInfo) {
								CLog.i(TAG, "getBaseInfo customerBean=" + customerBean.addInfo.toString());
								mView.customerInfoExist(type, customerBean);
							}else{
								mView.customerInfoNotExist(type, TextUtils.isEmpty(result.getRetMsg()) ? MSG_REAL_NAME_FAILED : result.getRetMsg());
							}
							break;
					}
				}else{
					if(type.equals(Constants.CUSTOMER_TYPE_IDENTITY)) {
						Global.LOGIN_REAL_NAME = "";
					}
					mView.customerInfoNotExist(type, TextUtils.isEmpty(result.getRetMsg()) ? MSG_REAL_NAME_FAILED : result.getRetMsg());
				}
			} else {
				if(type.equals(Constants.CUSTOMER_TYPE_IDENTITY)) {
					Global.LOGIN_REAL_NAME = "";
				}
				mView.getCustomerInfoError(type, TextUtils.isEmpty(result.getRetMsg()) ? MSG_REAL_NAME_FAILED : result.getRetMsg());
			}
		}
	}
}